package com.sanno.patient.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.query.MPJQueryWrapper;
import com.sanno.common.entity.SccPatient;
import com.sanno.common.entity.SncUserLogin;
import com.sanno.common.entity.vo.SccMedicalResultVoPatient;
import com.sanno.patient.dao.MedicalResultMapper;
import com.sanno.patient.dao.PatientMapper;
import com.sanno.patient.dao.UserMapper;
import com.sanno.patient.service.PatientService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;
import java.util.stream.Collectors;

@Service("patientService")
public class PatientServiceImpl extends ServiceImpl<PatientMapper, SccPatient> implements PatientService {
    @Resource
    private PatientMapper patientMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private MedicalResultMapper medicalResultMapper;

    /**
     * 查询检测者
     * @param sccPatient 查询的数据
     * @param page 查询的页数
     * @return
     */
    @Override
    public IPage<SccPatient> list(SccPatient sccPatient, Page<SccPatient> page) {
        MPJQueryWrapper<SccPatient> objectMPJQueryWrapper = new MPJQueryWrapper<>();
        objectMPJQueryWrapper.selectAll(SccPatient.class);
        objectMPJQueryWrapper.select("Store.store_name");
        objectMPJQueryWrapper.leftJoin("scc_drugstore Store on t.patient_drugstore=Store.Store_id");
        objectMPJQueryWrapper.eq("Status",1);
        if (sccPatient==null){
            return patientMapper.selectJoinPage(page,SccPatient.class, objectMPJQueryWrapper);
        }
        Map<String, Object> hutoolMap = BeanUtil.beanToMap(sccPatient);
        Map<String, Object> collect = hutoolMap.entrySet().stream().filter((e) -> e.getValue() != null&&e.getValue()!="").collect(Collectors.toMap(
                (e) -> e.getKey(),
                (e) -> e.getValue()));
        System.out.println(collect);

        for (Map.Entry<String, Object> entry : collect.entrySet()) {
            objectMPJQueryWrapper.like(entry.getKey(), entry.getValue());
        }
        IPage<SccPatient> page1 = patientMapper.selectJoinPage(page,SccPatient.class, objectMPJQueryWrapper);
        return page1;
    }

    /**
     * 添加检测者
     * @param sccPatient 添加数据
     * @return
     */
    @Override
    public Integer add(SccPatient sccPatient) {
        QueryWrapper<SncUserLogin> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("Phone",sccPatient.getPhone());
        SncUserLogin sncUserLogin = userMapper.selectOne(objectQueryWrapper);
        if (sncUserLogin!=null){
            sccPatient.setUserId(sncUserLogin.getId().toString());
            return patientMapper.insert(sccPatient);
        }
        SncUserLogin sncUserLogin1 = new SncUserLogin();
        sncUserLogin1.setName(sccPatient.getName());
        sncUserLogin1.setPhone(sccPatient.getPhone());
        int insert = userMapper.insert(sncUserLogin1);

        sccPatient.setUserId(sncUserLogin1.getId().toString());
        return patientMapper.insert(sccPatient);
    }

    /**
     * 删除检测者
     * @param arr 删除的id
     * @return
     */
    @Override
    public Integer delete(String arr) {
        QueryWrapper<SccPatient> queryWrapper=new QueryWrapper<>();
        SccPatient sccPatient = new SccPatient();
        sccPatient.setStatus(0);
        queryWrapper.in("Patient_id", Convert.toList(arr));
        int update = patientMapper.update(sccPatient, queryWrapper);
        return update;
    }

    /**
     * 修改检测者
     * @param sccPatient 修改的数据
     * @return
     */
    @Override
    public Integer update(SccPatient sccPatient) {
        QueryWrapper<SccPatient> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("Patient_id",sccPatient.getPatientId());
        return patientMapper.update(sccPatient, queryWrapper);
    }

    @Override
    public Integer addResult(SccMedicalResultVoPatient sccMedicalResult) {

        QueryWrapper<SccPatient> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("Patient_id",sccMedicalResult.getPatientId());
        SccPatient sccPatient = patientMapper.selectOne(queryWrapper);
        sccPatient.setCountOfTest(sccPatient.getCountOfTest()+1);
        sccPatient.setWeight(sccMedicalResult.getWeight());
        sccPatient.setHeight(sccMedicalResult.getHeight());
        patientMapper.update(sccPatient,queryWrapper);
        int insert = medicalResultMapper.insert(sccMedicalResult);
        return insert;
    }


}
